"""
自定义滑块控件
"""
import tkinter as tk
import threading


def passed():
    pass


class AUTOBIND_Operator:
    """
        自动绑定规则
    """

    AUTO_VALUE = "THIS IS AUTO" # 自动获取SuperScale(getResult)


class SuperScale:
    def __init__(self, win, minValue=1, maxValue=100, UnActiveColor='black', ActiveColor='white', isShowValue=False,
                 FontColor="red", width=300, height=20, default=None, ChageEventFunction=passed, Arg=None):
        """
        WAINING
            请不要使用place的width height放置这个组件，而是使用width 与 height定义
        @param win : 窗口
        @param minValue: 最小值
        @param maxValue: 最大值
        @param UnActiveColor:  背景色
        @param ActiveColor:  激活背景色
        @param isShowValue:  是否展示数值
        @param FontColor: 字体颜色
        @param width: 宽度
        @param height: 高度
        @param default: 默认值
        @param ChageEventFunction: 修改时间函数
        @param Arg: 修改时间函数参数
        """
        self.win = win
        self.default = default if default else minValue
        self.widthScaleContainer = width
        self.heightScaleContainer = height
        self.Arg = Arg
        self.Min, self.Max = minValue, maxValue
        self.ActiveColor = ActiveColor
        self.isShow = isShowValue
        self.UnActiveColor = UnActiveColor
        self.FontColor = FontColor
        self.Container = tk.Frame(bg=self.UnActiveColor)
        self.Container['height'] = self.heightScaleContainer
        self.Container['width'] = self.widthScaleContainer


        self.Inner2 = tk.Label(self.Container, bg=self.UnActiveColor, fg=self.FontColor)
        self.Inner = tk.Label(self.Container, bg=self.ActiveColor, fg=self.FontColor)


        self.Inner['text'] = default
        self.ChageEventFunction = ChageEventFunction
        temp_width = ((self.default - self.Min) / (self.Max - self.Min)) * self.widthScaleContainer
        print(temp_width)
        self.Inner.place(x=0, height=self.heightScaleContainer, width=temp_width, y=0)
        if self.isShow:
            if temp_width > 50:
                self.Inner2['text'] = ""
                self.Inner["text"] = self.default
            else:
                self.Inner2['text'] = self.default
                self.Inner2.place(x=self.widthScaleContainer - 100, height=self.heightScaleContainer, width=100, y=0)

                self.Inner['text'] = ""

        # TODO 控件绑定区

        self.Inner.bind("<B1-Motion>", self.MotionEvent)
        self.Inner.bind("<Button-1>", self.MotionEvent)
        self.Inner2.bind("<B1-Motion>", lambda e: self.MotionEvent(e, self.widthScaleContainer - 100))
        self.Inner2.bind("<Button-1>", lambda e: self.MotionEvent(e, self.widthScaleContainer - 100))
        self.Container.bind("<B1-Motion>", self.MotionEvent)
        self.Container.bind("<Button-1>", self.MotionEvent)

        # TODO 高级逻辑区

        self.BindAuto = False
        # 需要跟随绑定吗 : 更改一次数据重新进行一次绑定 一旦传入的参数元组存在 自动绑定操作符元素 就会自动触发
        self.AutoValue = []
        # 自动获取SuperScale(getResult)的跟随绑定

    def ChageEvent(self):
        if self.Arg == None:
            self.ChageEventFunction()
        else:
            t1 = threading.Thread(target=self.ChageEventFunction, args=self.Arg)
            t1.run()
            # t1.join()

    def BindChage(self, func, arg=None):

        self.ChageEventFunction = func
        self.Arg = arg

        # ------------- AUTO_VALUE ---------------
        for index,value in enumerate(arg):
            if value == AUTOBIND_Operator.AUTO_VALUE:
                self.AutoValue.append(index)
                self.BindAuto = True

        self.Arg = tuple([i if i != AUTOBIND_Operator.AUTO_VALUE else self.getResult() for i in self.Arg])
        myTmp = list(self.Arg)

        for i in self.AutoValue:
            myTmp[i] = self.getResult()

        self.Arg = myTmp
        # ------------ END AUTO_VALUE -------------------




    def MotionEvent(self, event, base=0):

        print(event.x)
        x = event.x + base
        if x > self.widthScaleContainer:

            x = self.widthScaleContainer
        elif x < 0:

            x = 0
        # print(x)
        VALUE = self.Min + int((self.Max - self.Min) / self.widthScaleContainer * x)
        self.Inner2.place(x=self.widthScaleContainer - 100, height=self.heightScaleContainer, width=100, y=0)
        self.Inner.place(x=0, height=self.heightScaleContainer, width=x, y=0)
        if self.isShow:
            if x > 50:
                self.Inner2['text'] = ""
                self.Inner["text"] = VALUE
            else:
                self.Inner2['text'] = VALUE
                self.Inner['text'] = ""
        if self.BindAuto:
            self.BindChage(self.ChageEventFunction,self.Arg)
        self.ChageEvent()

        # print(f"VALUE:{VALUE}")

    def getContainer(self):
        return self.Container

    def getResult(self):
        return self.Inner2['text'] if self.Inner2['text'] != "" else self.Inner['text']


if __name__ == '__main__':
    win = tk.Tk()
    win.geometry('500x500')
    Manager = SuperScale(win, -100, 530, "white", "green", True, "red", default=120, ChageEventFunction=print,
                         Arg=("你好",))
    Wid = Manager.getContainer()
    Wid.pack()
    tk.Button(text="get", command=lambda: print(Manager.getResult())).pack()
    win.mainloop()
